package com.yifeixi.validation.config;

import cn.hutool.core.collection.CollUtil;
import java.util.Locale;
import javax.validation.Validation;
import javax.validation.Validator;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.hibernate.validator.resourceloading.AggregateResourceBundleLocator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * validation配置<br> 使用自定义消息插值器
 *
 * @author YiFeiXi
 */
@Configuration
public class ValidationConfig {

  @Bean
  public Validator getValidator() {
    Locale.setDefault(Locale.CHINESE);
    return Validation.byDefaultProvider()
        .configure()
        .messageInterpolator(
            new MessageInterpolatorAdapter(
                new ResourceBundleMessageInterpolator(
                    new AggregateResourceBundleLocator(CollUtil.newArrayList("messages")))))
        .buildValidatorFactory()
        .getValidator();
  }
}
